/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package photoshare.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import photoshare.dto.AlbumDTO;
import photoshare.util.DBUtil;

/**
 *
 * @author PhotoShare
 */
public class AlbumDAO {

    private String READ_BY_ALL = "SELECT * FROM ALBUMS";
    private String READ_BY_ID = "SELECT * FROM ALBUMS WHERE AlbumID = ?";
    private String CREATE = "INSERT INTO ALBUMS VALUES(?,?,?)";
    private String DELETE_BY_ID = "UPDATE ALBUMS SET IsACTIVE= 0 WHERE AlbumID = ?";
    private String UPDATE_BY_ID = "UPDATE ALBUMS SET PASS = ? WHERE AlbumID = ?";

    private Connection conn = null;
    private PreparedStatement pst = null;

    public AlbumDAO() {
    }

    public boolean create(AlbumDTO albumDTO) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(CREATE);
            pst.setString(1, albumDTO.getName());
            pst.setString(2, albumDTO.getDescription());
            pst.setString(3, albumDTO.getDateCreate());
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public ArrayList<AlbumDTO> readAll() {
        try {
            ArrayList<AlbumDTO> arr = null;
            arr = new ArrayList<AlbumDTO>();
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ALL);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                AlbumDTO dto = new AlbumDTO();
                dto.setName(rs.getString(2));
                dto.setDescription(rs.getString(3));
                dto.setDateCreate(rs.getString(4));
                arr.add(dto);
            }
            return arr;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public AlbumDTO readById(Integer albumId) {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ID);
            pst.setInt(1, albumId);
            //pst = conn.prepareStatement("select * from acc WHERE accountID = 'admin'");
            ResultSet rs = pst.executeQuery();
            AlbumDTO albumDTO = new AlbumDTO();
            while (rs.next()) {
                albumDTO.setName(rs.getString(2));
                albumDTO.setDescription(rs.getString(3));
                albumDTO.setDateCreate(rs.getString(4));
            }
            return albumDTO;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public boolean updateById(AlbumDTO albumDTO, Integer albumId) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(UPDATE_BY_ID);
            //Tham so can Update trong cau lenh SQL
            pst.setString(1, albumDTO.getName());
            //Tham so dieu kien cua cau lenh Update
            pst.setInt(2, albumId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public boolean deleteById(Integer albumId) throws SQLException {

        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(DELETE_BY_ID);
            //Tham so dieu kien cho cau lenh Update
            pst.setInt(1, albumId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }
    

}
